package com.ctshk.app.system.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.starvip.dto.UserMemberBasicInformationDTO;
import com.ctshk.rpc.starvip.req.TransactionIntegralReq;
import com.ctshk.rpc.starvip.req.UserMemberBasicInformationReq;
import com.ctshk.rpc.starvip.service.IStarVipBusinessService;
import com.ctshk.rpc.system.dto.member.ContactQueryDTO;
import com.ctshk.rpc.system.dto.member.CrmAccountDTO;
import com.ctshk.rpc.system.req.crm.member.CrmMemberQueryReq;
import com.ctshk.rpc.system.req.crm.member.CrmMemberReq;
import com.ctshk.rpc.system.req.crm.member.CrmMemberTestReq;
import com.ctshk.rpc.system.req.crm.member.CrmMemberUpdatePointsReq;
import com.ctshk.rpc.system.service.ICrmMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;

/**
 * <p>
 * crm会员 前端控制器
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-26
 */
@Api(value = "crm会员管理", tags = "crm会员管理")
@RestController
@RequestMapping("/crm/member")
public class CrmMemberController extends BaseController {

    @DubboReference
    private ICrmMemberService crmMemberService;

    @DubboReference
    private IStarVipBusinessService businessService;

    @ApiOperation(value = "crm会员查询", notes = "crm会员查询")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "crm会员查询", operDesc = "crm会员查询")
    @PostMapping("/member/get")
    public Result<ContactQueryDTO> queryMember(@RequestBody @Valid CrmMemberQueryReq crmMemberQueryReq) {
        ContactQueryDTO contactQueryDTO = crmMemberService.queryMember(crmMemberQueryReq);
        return Result.success(contactQueryDTO);
    }

    @ApiOperation(value = "查询crm账号信息", notes = "查询crm账号信息")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "查询crm会员详细", operDesc = "查询crm会员详细")
    @PostMapping("/account/get")
    public Result<CrmAccountDTO> queryDetail(@RequestBody @Valid CrmMemberQueryReq crmMemberQueryReq) {
        CrmAccountDTO crmAccountDTO = crmMemberService.queryAccountDetail(crmMemberQueryReq);
        return Result.success(crmAccountDTO);
    }

    @ApiOperation(value = "获取会员积分兑港币汇率", notes = "获取会员积分兑港币汇率")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "获取会员积分兑港币汇率", operDesc = "获取会员积分兑港币汇率")
    @GetMapping("/points/to/hkd")
    public Result<BigDecimal> queryPointsToHkd() {
        BigDecimal amountHkd = crmMemberService.queryPointsToHkd();
        return Result.success(amountHkd);
    }

    @ApiOperation(value = "更新CRM会员积分TEST", notes = "更新CRM会员积分TEST")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "更新CRM会员积分", operDesc = "更新CRM会员积分")
    @PostMapping("/update/test")
    public Result updateTest(@RequestBody @Valid CrmMemberTestReq crmMemberTestReq) {
        CrmMemberQueryReq crmMemberQueryReq = EntityUtil.copy(crmMemberTestReq, CrmMemberQueryReq.class);
        ContactQueryDTO contactQueryDTO = crmMemberService.queryMember(crmMemberQueryReq);
        CrmMemberUpdatePointsReq crmMemberUpdatePointsReq = new CrmMemberUpdatePointsReq();
        crmMemberUpdatePointsReq.setMemberId(contactQueryDTO.getConCellPhNum());
        crmMemberUpdatePointsReq.setUpdateType(crmMemberTestReq.getUpdateTYpe());
        crmMemberUpdatePointsReq.setNumPoints(crmMemberTestReq.getNumPoints());//do积分点数
        crmMemberUpdatePointsReq.setTxnNum(crmMemberTestReq.getTxnNum());//do交易編號 (如為旅行團交易, 目前我們的格式是 旅行團交易編號 + ‘-‘ + 序號, 例如: 01P08192131025A155506-1
        crmMemberUpdatePointsReq.setXCtsProdDesc("單項委託-火车巴士船票(机场快线)");//do板塊產品描述 (建議使用: 集團產品名稱 + “ – “ + 板塊產品名稱, 例如: 港澳参团游 - 主题游)")
        BigDecimal amtVal = new BigDecimal("100");
        BigDecimal rate = new BigDecimal("1.2");
        BigDecimal rmbAmount = amtVal.divide(rate, 2, BigDecimal.ROUND_HALF_UP);
        crmMemberUpdatePointsReq.setAmtVal(amtVal);
        crmMemberUpdatePointsReq.setXCtsRmbAmount(rmbAmount);
        crmMemberUpdatePointsReq.setCreateUserid("1");
        crmMemberUpdatePointsReq.setXCtsTxnOrgCode("219910000010");
        crmMemberUpdatePointsReq.setTxnChannelCd("16");
        crmMemberUpdatePointsReq.setXCtsTxnChnNum("211099000010");
        crmMemberUpdatePointsReq.setProdName("8821090000");
        crmMemberUpdatePointsReq.setXCtsProdCode("8821090300");
        CrmMemberReq crmMemberReq = crmMemberService.updatePoints(crmMemberUpdatePointsReq, contactQueryDTO);
        return Result.success(crmMemberReq);
    }

    @ApiOperation(value = "更新CRM会员积分TEST", notes = "更新CRM会员积分TEST")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "更新CRM会员积分", operDesc = "更新CRM会员积分")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid CrmMemberUpdatePointsReq crmMemberUpdatePointsReq) {
//        crmMemberUpdatePointsReq.setUpdateType(crmMemberQueryReq.getUpdateType());
//        crmMemberUpdatePointsReq.setTxnNum(crmMemberQueryReq.getTxnNum());//do交易編號 (如為旅行團交易, 目前我們的格式是 旅行團交易編號 + ‘-‘ + 序號, 例如: 01P08192131025A155506-1
        crmMemberUpdatePointsReq.setXCtsProdDesc("單項委託-火车巴士船票(机场快线)");//do板塊產品描述 (建議使用: 集團產品名稱 + “ – “ + 板塊產品名稱, 例如: 港澳参团游 - 主题游)")
        BigDecimal amtVal = new BigDecimal("100");
        BigDecimal rate = new BigDecimal("1.2");
        BigDecimal rmbAmount = amtVal.divide(rate, 2, BigDecimal.ROUND_HALF_UP);
        crmMemberUpdatePointsReq.setAmtVal(amtVal);
        crmMemberUpdatePointsReq.setXCtsRmbAmount(rmbAmount);
        crmMemberUpdatePointsReq.setCreateUserid("1");
        crmMemberUpdatePointsReq.setXCtsTxnOrgCode("219910000010");
        crmMemberUpdatePointsReq.setTxnChannelCd("16");
        crmMemberUpdatePointsReq.setXCtsTxnChnNum("211099000010");
        crmMemberUpdatePointsReq.setProdName("8821090000");
        crmMemberUpdatePointsReq.setXCtsProdCode("8821090300");
        CrmMemberReq crmMemberReq = crmMemberService.updatePoints(crmMemberUpdatePointsReq);
        return Result.success(crmMemberReq);
    }

    @ApiOperation(value = "更新CRM会员积分TEST2", notes = "更新CRM会员积分TEST2")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "更新CRM会员积分2", operDesc = "更新CRM会员积分2")
    @PostMapping("/update2")
    public Result update2(@RequestBody @Valid CrmMemberUpdatePointsReq crmMemberUpdatePointsReq) {
        CrmMemberReq crmMemberReq = crmMemberService.updatePointsByStarVip(crmMemberUpdatePointsReq);
        return Result.success(crmMemberReq);
    }

    @PostMapping("/star/member/search")
    @ApiOperation(value = "获取会员基本信息", notes = "获取会员基本信息")
    @OperationLogAnnotation(operType = OperationLogType.QUERY, operTitle = "获取会员基本信息", operDesc = "获取会员基本信息")
    public Result<UserMemberBasicInformationDTO> getUserMemberBasicInformation(@RequestBody UserMemberBasicInformationReq req) {
        return businessService.getUserMemberBasicInformation(req);
    }

    @PostMapping("/star/transactionIntegral")
    @ApiOperation(value = "积分交易", notes = "积分交易")
    @OperationLogAnnotation(operType = OperationLogType.UPDATE, operTitle = "积分交易", operDesc = "积分交易")
    public Result transactionIntegral(@RequestBody TransactionIntegralReq req){
        return businessService.transactionIntegral(req);
    }

    @GetMapping("/star/hkd/{point}")
    @ApiOperation(value = "积分兑换港币", notes = "积分兑换港币")
    @OperationLogAnnotation(operType = OperationLogType.QUERY, operTitle = "积分兑换港币", operDesc = "积分兑换港币")
    public Result getHKD(@PathVariable Integer point) {
        return Result.success(businessService.getHkd(point));
    }
}
